//
//  ForumViewModel.m
//  MoXiProject
//
//  Created by 沈晨宇 on 2017/7/31.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "ForumViewModel.h"

@implementation ForumViewModel

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.allForumArrayList = [[NSMutableArray alloc] initWithCapacity:0];
        self.forumDisArrayList = [[NSMutableArray alloc] initWithCapacity:0];
//        self.noticeArrayList = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return self;
}

//发布论坛
- (RACSignal *)saveUserForumRequestParameter:(NSString *)userId title:(NSString *)title content:(NSString *)content{
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary * param = @{
                                 @"userId"    :userId,
                                 @"title"    :title,
                                 @"content"    :content
                                 };
        
        [RequestOperationManager SaveUserForumParametersDic:param success:^(NSDictionary *result) {
            
            if (result) {
                
                
            }
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
        } failture:^(id result) {
            
      
            [subscriber sendError:result];
        }];
        
        return nil;
    }];

}

//获取论坛接口
- (RACSignal *)forumListRequestParameter:(LoadType)loadType{
    
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        self.page +=10;
        NSDictionary * parma = @{@"pageNo"     :[NSString stringWithFormat:@"0"],
                                 @"pageSize"   :[NSString stringWithFormat:@"%ld",self.page],
                                 @"userId"    : @""};
        
        [RequestOperationManager ForumListParametersDic:parma success:^(NSDictionary *result) {
            
            if (result) {
                 NSLog(@"所有论坛：%@",result);
                if (loadType == LoadData) {
                    [self.allForumArrayList removeAllObjects];
                }

                [self.allForumArrayList addObjectsFromArray:(NSMutableArray *)[MTLJSONAdapter modelsOfClass:[AllForumModel class] fromJSONArray:result[@"result"] error:nil]];
               
            }
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
        } failture:^(id result) {
            
            [self.allForumArrayList removeAllObjects];
            [subscriber sendError:result];
        }];
        
        return nil;
    }];

}

//论坛详情接口
- (RACSignal *)forumsDetailRequestParameter:(NSString *)forumId userId:(NSString *)userId {

    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary * param = @{
                                 @"forumId"    :forumId,
                                 @"userId"    :userId,
                                 };
        
        [RequestOperationManager ForumsDetailParametersDic:param success:^(NSDictionary *result) {
            
            if (result) {
                 [self.forumDisArrayList addObjectsFromArray:(NSMutableArray *)[MTLJSONAdapter modelsOfClass:[ForumDisModel class] fromJSONArray:result[@"discussList"] error:nil]];
                NSLog(@"论坛详情：%@",result);
            }
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
        } failture:^(id result) {
            
            
            [subscriber sendError:result];
        }];
        
        return nil;
    }];

}

////添加论坛评论接口
- (RACSignal *)addForumDiscussRequestParameter:(NSString *)forumId content:(NSString *)content userId:(NSString *)userId{

    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary * param = @{
                                 @"forumId"    :forumId,
                                 @"content"    :content,
                                 @"userId"    :userId
                                 };
        
        [RequestOperationManager AddForumDiscussParametersDic:param success:^(NSDictionary *result) {
            
            if (result) {
                
                
               
            }
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
        } failture:^(id result) {
            
            
            [subscriber sendError:result];
        }];
        
        return nil;
    }];

}

////论坛点赞接口
- (RACSignal *)saveForumThumbsRequestParameter:(NSString *)userId forumId:(NSString *)forumId{
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        NSDictionary * param = @{
                                 @"userId"    :userId,
                                 @"forumId"    :forumId,
                                 };
        
        [RequestOperationManager SaveForumThumbsParametersDic:param success:^(NSDictionary *result) {
            
            if (result) {
                
                
            }
            
            [subscriber sendNext:result];
            [subscriber sendCompleted];
        } failture:^(id result) {
            
            
            [subscriber sendError:result];
        }];
        
        return nil;
    }];

}

@end
